using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NCS.Library.Components.Modules;
using NCS.Library.Components.Security;
using NCS.Library.Components.Tabs;
using NCS.Library.Components.Portal;
using NCS.Library.Utilities;
using NCS.Library;
using Telerik.WebControls;

namespace Portal.Administrator.Tabs
{
	public partial class TabDesign : AuthenticatedControl
	{
        private int tabId; 
        private int portalId;
		private TabLayoutControl layout;

		protected void Page_Load(object sender, EventArgs e)
		{
			tabId = ConvertUtility.ToInt32(Request.QueryString["tabid"]);
            portalId = ConvertUtility.ToInt32(Request.QueryString["portalid"]);

			if (tabId == 0) tabId = ConvertUtility.ToInt32(dropTab.SelectedValue);
			if (!Page.IsPostBack)
			{               
                //Fill portals

                dropPortal.Items.Clear();
                PortalController.LoadPortals(dropPortal.Items);

                if (!CurrentUser.IsSuperAdmin && !CurrentUser.IsAdmin)
                {
                    dropPortal.Enabled = false;
                }
                dropPortal.Items.Insert(0, new ListItem("Root", "0"));
                dropPortal.SelectedValue = AppEnv.PortalId();
			    
                //dropPortal.DataSource = PortalController.GetPortalsByLang(AppEnv.GetLanguage());
                //dropPortal.DataBind();
                //dropPortal.SelectedValue = AppEnv.PortalId();

                if (!CurrentUser.IsSuperAdmin && !CurrentUser.IsAdmin)
                {
                    dropPortal.Enabled = false;
                }

                TabController.FillToList(dropTab.Items, TabController.GetTabsByPortalID(ConvertUtility.ToInt32(dropPortal.SelectedValue)));
                dropTab.Items.Insert(0, new ListItem("Select a tab", "0"));

                dropModuleControl.DataSource = ModuleControlController.GetModuleControlsForWeb();
                dropModuleControl.DataBind();

                MiscUtility.SelectItemFromList(dropTab, tabId.ToString());                

			}
			
			string selectedPane = dropPane.SelectedValue;
			dropPane.Items.Clear();
			
			TabInfo tab = TabController.GetTab(tabId);
			if (tab != null && tab.TabLayout != string.Empty)
			{
				//Load layout and panes
          
                layout = (TabLayoutControl)LoadControl(tab.TabLayout);
				
				if (layout != null)
				{
					PlaceHolder1.Controls.Add(layout);
					foreach (RadDockingZone zone in layout.Zones)
					{
						zone.BackColor = Color.FromName("LightGray");
						Literal tmp = new Literal();
						tmp.Text = zone.ID;
						zone.Controls.Add(tmp);
						zone.BorderWidth = 1;
						dropPane.Items.Add(new ListItem(zone.ID, zone.ID));
					}
					MiscUtility.SelectItemFromList(dropPane, selectedPane);
				}
				//Load control for cur tab
				DataTable dtControls = ModuleControlController.GetModuleControls(tabId, string.Empty);
				foreach(DataRow row in dtControls.Rows)
				{
					CreateDockObject(row);
				}
			}
		}
		protected void dropTab_SelectedIndexChanged(object sender, EventArgs e)
		{
			Response.Redirect(AppEnv.ADMIN_CMD + "?cmd=tab_design&portalid=" +dropPortal.SelectedValue+ "&tabid=" + dropTab.SelectedValue);
		}
		protected void btnAdd_Click(object sender, EventArgs e)
		{
			TabController.AddControlToTab(0, tabId, Convert.ToInt32(dropModuleControl.SelectedValue), dropPane.SelectedValue, 0);
			Response.Redirect(Request.RawUrl);
		}

		protected RadDockableObject CreateDockObject(DataRow control)
		{
			RadDockableObject dockableObject = new RadDockableObject();
			dockableObject.ID = control["TabControlID"].ToString();
            dockableObject.Behavior = RadDockableObjectBehaviorFlags.Resizable | RadDockableObjectBehaviorFlags.None | RadDockableObjectBehaviorFlags.Close | RadDockableObjectBehaviorFlags.Collapse;
			dockableObject.DockingMode = RadDockingModeFlags.AlwaysDock;
			dockableObject.ContentTemplate = Page.LoadTemplate(control["ControlPath"].ToString());
			dockableObject.Width = Unit.Percentage(100);
			dockableObject.Height = Unit.Pixel(20);
			RadDockingZone zone = (RadDockingZone)layout.FindControl(control["LayoutPosition"].ToString());
			dockableObject.Text = control["ControlTitle"].ToString();
			dockableObject.BorderWidth = 1;
			if(zone != null)
				zone.Controls.Add(dockableObject);
			return dockableObject;
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			RadDockingManager manager = RadDockingManager.GetCurrentRadDockingManager(this.Page);
			manager.SaveState();
			foreach(RadDockableObject dockObj in manager.DockableObjects)
			{
				if(!dockObj.Closed)
					TabController.UpdateTabControl(Convert.ToInt32(dockObj.ID), Convert.ToInt32(tabId), dockObj.ParentDockingZone.ID, dockObj.ParentDockingZone.DockableObjects.IndexOf(dockObj));
				else
					TabController.RemoveControlFromTab(Convert.ToInt32(dockObj.ID));
				
			}
			Response.Redirect(Request.RawUrl);
		}

        protected void dropPortal_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(AppEnv.ADMIN_CMD + "?cmd=tab_design&portalid=" + dropPortal.SelectedValue);
        }
	}
}